<template>
  <div class="login-container">
    <!-- 成功提示框 -->
    <div v-if="showSuccess" class="success-toast">
      <div class="success-content">
        <!-- <div class="success-icon">✓</div> -->
        <img src="../assets/logo.svg" alt="" />
        <span>登录成功！</span>
      </div>
    </div>

    <div class="login-box">
      <h2 class="login-title">管理系统</h2>
      <div class="login-form">
        <div class="form-item">
          <label>用户名</label>
          <input
            type="text"
            v-model="admin"
            placeholder="请输入用户名"
            class="form-input"
            @keyup.enter="Dl"
          />
        </div>
        <div class="form-item">
          <label>密码</label>
          <input
            type="password"
            v-model="password"
            placeholder="请输入密码"
            class="form-input"
            @keyup.enter="Dl"
          />
        </div>
        <div class="form-actions">
          <button class="login-btn" @click="Dl" :disabled="loading">
            <span v-if="loading" class="loading-spinner"></span>
            {{ loading ? '登录中...' : '登录' }}
          </button>
          <button class="reset-btn" @click="handleReset">重置</button>
        </div>
      </div>
    </div>

    <!-- 背景装饰 -->
    <div class="bg-decoration">
      <div class="cube cube1"></div>
      <div class="cube cube2"></div>
      <div class="cube cube3"></div>
      <div class="cube cube4"></div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data() {
    return {
      admin: '',
      password: '',
      loading: false,
      showSuccess: false,
    }
  },
  methods: {
    async login() {
      try {
        const res = await axios.post('http://47.94.6.186:3001/admin/admin/login', {
          adminname: this.admin,
          password: this.password,
        })
        console.log(res)

        // 如果API返回成功，存储token
        if (res.data && res.data.data && res.data.data.token) {
          localStorage.setItem('loginToken', res.data.data.token)
          this.showSuccess = true
          setTimeout(() => {
            this.loading = false
            this.$router.push('/')
          }, 1500)
        }
        return res
      } catch (error) {
        console.error('登录请求失败:', error)
        throw error
      }
    },
    async Dl() {
      if (!this.admin || !this.password) {
        this.showErrorMessage('请输入用户名和密码')
        return
      }

      this.loading = true

      try {
        // 首先尝试API登录
        await this.login()
      } catch  {
        // API登录失败，使用本地验证
        setTimeout(() => {
          if (this.admin === 'admin' && this.password === '123456') {
            // 本地登录成功，也存储一个模拟token
            localStorage.setItem('loginToken', 'mock-token-' + Date.now())
            this.showSuccess = true
            setTimeout(() => {
              this.loading = false
              this.$router.push('/')
            }, 1500)
          } else {
            this.loading = false
            this.showErrorMessage('用户名或密码错误')
          }
        }, 800)
      }
    },
    handleReset() {
      this.admin = ''
      this.password = ''
    },
    showErrorMessage(message) {
      // 创建错误提示
      const errorToast = document.createElement('div')
      errorToast.className = 'error-toast'
      errorToast.innerHTML = `
        <div class="error-content">
          <div class="error-icon">✕</div>
          <span>${message}</span>
        </div>
      `
      document.body.appendChild(errorToast)

      setTimeout(() => {
        errorToast.remove()
      }, 3000)
    },
  },
  mounted() {},
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.login-box {
  width: 420px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  padding: 40px;
  z-index: 10;
  animation: slideIn 0.6s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-title {
  text-align: center;
  margin-bottom: 35px;
  color: #333;
  font-size: 26px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.form-item {
  margin-bottom: 25px;
}

.form-item label {
  display: inline-block;
  width: 70px;
  text-align: right;
  margin-right: 15px;
  color: #555;
  font-weight: 500;
}

.form-input {
  width: calc(100% - 90px);
  padding: 12px 15px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  outline: none;
  font-size: 14px;
  transition: all 0.3s ease;
}

.form-input:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-actions {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 35px;
}

.login-btn,
.reset-btn {
  padding: 12px 30px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-btn {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  min-width: 100px;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}

.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.loading-spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-right: 8px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.reset-btn {
  background-color: #f8f9fa;
  color: #6c757d;
  border: 2px solid #e9ecef;
}

.reset-btn:hover {
  background-color: #e9ecef;
  transform: translateY(-2px);
}

/* 成功提示框 */
.success-toast {
  position: fixed;
  top: 50px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, #4caf50, #45a049);
  color: white;
  padding: 15px 25px;
  border-radius: 10px;
  box-shadow: 0 8px 25px rgba(76, 175, 80, 0.3);
  z-index: 1000;
  animation: slideDown 0.5s ease-out;
}

.success-content {
  display: flex;
  align-items: center;
  gap: 10px;
}

.success-icon {
  width: 24px;
  height: 24px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* 错误提示框 */
:global(.error-toast) {
  position: fixed;
  top: 50px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, #f44336, #d32f2f);
  color: white;
  padding: 15px 25px;
  border-radius: 10px;
  box-shadow: 0 8px 25px rgba(244, 67, 54, 0.3);
  z-index: 1000;
  animation: slideDown 0.5s ease-out;
}

:global(.error-content) {
  display: flex;
  align-items: center;
  gap: 10px;
}

:global(.error-icon) {
  width: 24px;
  height: 24px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

/* 背景装饰 */
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.cube {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  transform: rotate(45deg);
}

.cube1 {
  width: 80px;
  height: 80px;
  top: 20%;
  left: 15%;
  animation: float 6s infinite ease-in-out;
}

.cube2 {
  width: 120px;
  height: 120px;
  top: 60%;
  right: 20%;
  animation: float 8s infinite ease-in-out reverse;
}

.cube3 {
  width: 60px;
  height: 60px;
  bottom: 20%;
  left: 20%;
  animation: float 7s infinite ease-in-out;
}

.cube4 {
  width: 100px;
  height: 100px;
  top: 15%;
  right: 15%;
  animation: float 9s infinite ease-in-out reverse;
}

@keyframes float {
  0%,
  100% {
    transform: rotate(45deg) translate(0, 0);
  }
  50% {
    transform: rotate(45deg) translate(15px, 15px);
  }
}
</style>
